//
//  YahooStockConfidenceRemainHtmlInterpreter.m
//  MarketDataColloctor
//
//  Created by jiro on 10/03/06.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "YahooStockConfidenceRemainHtmlInterpreter.h"

@implementation YahooStockConfidenceRemainHtmlInterpreter

- (id) interpret: (NSString*) html;
{
	StockConfidenceRemainJaList* list = [ [ StockConfidenceRemainJaList alloc ] init ];

	// ---- 前提条件チェック ---- //
	if ( [ html isEqualToString: @"" ] ||
		 [ html rangeOfString: @"この検索期間の信用残データはありません" ].location != NSNotFound ||
		 [ html rangeOfString: @"の信用残データはありません" ].location != NSNotFound )
	{
		return list;
	}
	// ----------------------- //

	NSError* error = nil;
	NSXMLDocument* document =
		[ [ NSXMLDocument alloc ] initWithXMLString: html options: NSXMLDocumentTidyHTML error: & error ];
	if ( error == nil )
	{
		errorType = HtmlInterpretError_ReadFile;
		return nil;
	}

	NSXMLElement* element = [ document rootElement ];
	NSXMLNode* node;

	//	brand code
	NSString* brandCode;
	node = [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ element childAtIndex: 1 ] childAtIndex: 0 ] childAtIndex: 0 ] childAtIndex:13 ] 
		childAtIndex: 1 ] childAtIndex: 0 ] childAtIndex: 0 ] childAtIndex: 1 ] childAtIndex: 0 ] childAtIndex: 1 ] 
		childAtIndex: 0 ] childAtIndex: 1 ] childAtIndex: 1 ] childAtIndex: 1 ] childAtIndex: 4 ];
	brandCode = [ [ [ (NSXMLElement*) node attributeForName: @"value" ] stringValue ] substringToIndex: 4 ];

	@try {

		//	---	信用残のある所までスキップ
		node = [ [ [ [ [ [ [ [ [ [ [ element childAtIndex: 1 ] childAtIndex: 0 ] childAtIndex: 0 ] childAtIndex: 15 ] 
			childAtIndex: 0 ] childAtIndex: 1 ] childAtIndex: 0 ] childAtIndex: 3 ] childAtIndex: 1 ] childAtIndex: 0 ]
			childAtIndex: 0 ];
		if ( [ node childCount ] <= 2 )
		{	//	信用残のデータなし
			return list;
		}

		NSArray* children = [ node children ];
		NSEnumerator* e = [ children objectEnumerator ];
		NSXMLNode* confidenceNode;
		NSString* value;
		StockConfidenceRemainJa* remain;
		[ e nextObject ];	//	skip unnecessary node
		[ e nextObject ];	//	skip unnecessary node
		while ( confidenceNode = [ e nextObject ] )
		{
//			NSLog( @"child count: %d", [ confidenceNode childCount ] );
//			for ( int i = 0; i < [ confidenceNode childCount ]; i++ )
//			{
//				NSLog( @"i = %d: \n%@", i, [ [ confidenceNode childAtIndex: i ] description ] );
//			}
			
			if ( [ confidenceNode childCount ] != 7 )
			{
				errorType = HtmlInterpretError_UnknownHtmlNode;
				return nil;
			}
			
			remain = [ [ StockConfidenceRemainJa alloc ] init ];

			remain.code = brandCode;

			//	exchange date
			value = [ [ [ confidenceNode childAtIndex: 0 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"年" withString: @"-" ];
			value = [ value stringByReplacingOccurrencesOfString: @"月" withString: @"-" ];
			value = [ value stringByReplacingOccurrencesOfString: @"日" withString: @"" ];
			remain.exchangeDate = [ NSDate dateWithString: [ NSString stringWithFormat: @"%@ 00:00:00 +0900", value ] ];

			//	sell remain
			value = [ [ [ confidenceNode childAtIndex: 1 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			remain.sellRemain = [ value floatValue ]; 

			//	buy remain
			value = [ [ [ confidenceNode childAtIndex: 2 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			remain.buyRemain = [ value floatValue ]; 

			//	sell variations
			value = [ [ [ confidenceNode childAtIndex: 3 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			remain.sellVariation = [ value floatValue ]; 

			//	sell variations
			value = [ [ [ confidenceNode childAtIndex: 4 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			remain.buyVariation = [ value floatValue ]; 

			//	confidence rate
			value = [ [ [ confidenceNode childAtIndex: 5 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			remain.confidenceRate = [ value floatValue ]; 
			
			[ list addObject: remain ];
		}
		//	---
	}
	@catch ( NSException* exception )
	{
		errorType = HtmlInterpretError_UnknownHtmlNode;
		NSLog( @"HTMLの解析に失敗　銘柄コード: %@", brandCode );
		return nil;
	}
	return list;
}

@end
